/**
 * Events
 **/

function getTotales(){
		var suma=0;
		var prodstot=0;
		
		$("li.qty").each(function(){
			
			prodstot++;
			var qty= ($(this).find("input.quantity").val());
			int_quantity = parseInt(qty);
			var price= ($(this).find("input.priceproduct").val());
			suma += int_quantity*price;
		});

		($('li.subtotalall').html("$ "+(suma).toFixed(2)));
		($('li.totalall').html("$ "+(suma).toFixed(2)));
		($('span#totalpricecrumbs').html("$"+(suma).toFixed(2)));
		($('span#totalproductscrumbs').html("COMPRAS: "+ prodstot));
		
	}

jQuery(document).ready(function(){
	
	$('span.addtocar').click(function(){
		
		var quantity= ($(this).parent().parent().parent().find('input#qty').val());
		
		int_quantity = parseInt(quantity);
		if(isNaN(int_quantity)){
			quantity=1;
		}else{
			quantity=int_quantity;
		}
		
		var url=$(this).parent().attr('href');
		var newurl= url+"quantity/"+quantity+"/";
		window.location=newurl;
		return false;
	});

	
	
	$('a.moreproductinshoppingcart').click(function(){
		var quantity= ($(this).parent().find('input.quantity').val());
		var price= ($(this).parent().find('input.priceproduct').val());
		
		int_quantity = parseInt(quantity);
		
		if(isNaN(int_quantity)){
			quantity=1;
		}else{
			quantity=int_quantity+1;
		}
		
		($(this).parent().find('input.quantity').val(quantity));
		($(this).parent().parent().find('li.totalprice').html("$"+(quantity*price).toFixed(2)));
		var id = ($(this).parent().find('input.idproduct').val());
		
		var urls = site+"cart/addproduct/";
		var datos = "productid="+id+"&quantity="+quantity+"&ajax=true";
		jQuery.ajax({
            type: "POST",
            url: urls,
            data: datos,
            success: function(msg){}
        });
		
		getTotales();
		return false;
	});

	
	
	$('a.minusproductinshoppingcart').click(function(){
		var quantity= ($(this).parent().find('input.quantity').val());
		var price= ($(this).parent().find('input.priceproduct').val());
		
		int_quantity = parseInt(quantity);
		
		if(isNaN(int_quantity)){
			quantity=1;
		}else{
			quantity=int_quantity-1;
		}
		if(quantity==0){
			quantity=1;
		}
		
		($(this).parent().find('input.quantity').val(quantity));
		($(this).parent().parent().find('li.totalprice').html("$"+(quantity*price).toFixed(2)));
		
		
		var id = ($(this).parent().find('input.idproduct').val());
		
		var urls = site+"cart/addproduct/";
		var datos = "productid="+id+"&quantity="+quantity+"&ajax=true";
		jQuery.ajax({
            type: "POST",
            url: urls,
            data: datos,
            success: function(msg){}
        });
		
		getTotales();
		return false;
	});
	
	
	$('a.removeproductinshoppingcart').click(function(){
		var quantity = ($(this).parent().find('input.quantity').val());
		var urls = site+"cart/removeproduct/";
		var este= $(this);
		int_quantity = parseInt(quantity);
		
		if(isNaN(int_quantity)){ quantity=1; }
		else{ quantity=int_quantity-1; }
		if(quantity==0){ quantity=1; }
		
		
			var id = ($(this).parent().parent().attr('id'));
			var datos = "productid="+id+"&";
			jQuery.ajax({
	            type: "POST",
	            url: urls,
	            data: datos,
	            success: function(msg){
	            	$("#"+id).remove();
	            	getTotales();
	            	//window.location=site+"cart/";
	            }
	        });
		
		return false;
	});

	
/*******************************************************/
    $('input#salvar').click(function() {
        var comentario = $('textarea#comentario').val();
        var id_user = $('input#id_user').val();
        var seccion = $('input#seccion').val();
        var galeria = $('input#galeria').val();

        var datos = "comentario="+comentario +"&user="+ id_user +"&seccion="+seccion + "&galeria="+galeria;

        $('textarea#comentario').val('');

        jQuery.ajax({
            type: "POST",
            url: site+"atencionaagentes/addcomentario/",
            data: datos,
            success: function(msg){
                $('div#comments').html(msg);
            }
        });

        $('div.comentarios-div').hide('fast');
      return false;
    });
});

function valEmail(valor){
    re=/^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$/
    if(!re.exec(valor))    {
        return false;
    }else{
        return true;
    }
}

function getestados(pais, estado){
	var estados="";
	var datos="idpais="+pais;
	var sitiourl=site+"users/getestadosbyidpais/";
	var items = [];
	var estado_default=estado;
	var selected="";
	$.ajax({
        type: "POST",
        url: sitiourl,
        dataType: "JSON",
        data: datos,
        beforeSend: function(){$("select#State").html('<option>Espere un momento</option>');},
        success: function(msg){
        	$.each(msg, function(key, val) {
        		selected= (estado_default==val.Id) ? 'selected="selected"' : '';
        	    items.push('<option value="' + val.Id + '" '+ selected +'>' + val.Estado + '</option>');
        	});
        	$("select#State").html(items.join(''));
        }
    });
	
}



